package com.xsh.service.impl;

import com.xsh.constant.Constant;
import com.xsh.mapper.UserMapper;
import com.xsh.pojo.User;
import com.xsh.service.UserService;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User findByUsername(String username) {
        return userMapper.selectByUsername(username);
    }

    @Override
    public void insert(User user) {
        String salt = UUID.randomUUID().toString();
        user.setSalt(salt);
        String password = new Sha256Hash(user.getPassword(), user.getSalt(), Constant.COUNT).toBase64();
        user.setPassword(password);
        userMapper.insert(user);
    }

}
